<?php
namespace app\erp\model;

use think\Model;
use traits\model\SoftDelete;
use app\erp\validate\Adsenserule;
use think\Cache;

class Adsensedb extends Model
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';

    protected $pk = 'adsense_id';

    protected $name = 'adsense';

    protected $type = [
        'create_time' => 'timestamp:Y-m-d',
        'delete_time' => 'timestamp:Y-m-d'
    ];
    // 自动完成时间转换
    protected $autoWriteTimestamp = true;

    protected $insert = [
        'partner_id' => PARTNER_ID,
        'adsense_name'
    ];
    // 自动完成名称去除空格
    protected function setAdsenseNameAttr($value)
    {
        return trim($value);
    }

    public $param = array();

    protected function cache_name($name)
    {
        $new_arr = $this->param;
        $new_arr['partner_id'] = PARTNER_ID;
        if (! isset($new_arr['site_id'])) {
            $new_arr['site_id'] = SITE_ID;
        }
        ksort($new_arr);
        $param_str = http_build_query($new_arr);
        return md5($name . $param_str);
    }

    public function scopeAdsenseName($query, $adsense_name)
    {
        $query->where('adsense_name', 'like', '%' . $adsense_name . '%');
    }
    // 基础查询
    public function base($query)
    {
        $field = $this->getDeleteTimeField(true);
        if ($field) {
            if (isset($this->param['delete']) && $this->param['delete'] == 'yes') {
                $this->onlyTrashed();
            } else {
                $query->useSoftDelete($field);
            }
        }
        $query->where('partner_id', PARTNER_ID);
        if (isset($this->param['adsense_name']) && $this->param['adsense_name'] != '') {
            $query->artclassName($this->param['adsense_name']);
        }
    }

    public function ajaxFind($adsense_id)
    {
        $this->param['adsense_id'] = $adsense_id;
        $data = $this->useGlobalScope(false)
            ->master()
            ->where('adsense_id', $this->param['adsense_id'])
            ->find();
        $data = $data->toArray();
        return $data;
    }
    // 查询
    public function ajaxSelect()
    {
        $return['data'] = $this->ajaxList();
        $return['count'] = $this->ajaxCount();
        return $return;
    }

    public function ajaxList()
    {
        $data = $this->order($this->param['_o'], $this->param['_b'])
            ->cache($this->cache_name('adsense_list'), 86400, 'adsense')
            ->page($this->param['page'], $this->param['limit'])
            ->select();
        if (! $data->isEmpty()) {
            $data = $data->toArray();
        }
        return $data;
    }

    public function ajaxCount()
    {
        $count = $this->cache($this->cache_name('adsense_count'), 86400, 'adsense')
            ->count();
        return $count;
    }

    public function ajaxSave()
    {
        $rule = new Adsenserule();
        $this->param['partner_id'] = PARTNER_ID;
        if (! $rule->check($this->param)) {
            return $rule->getError();
        }
        unset($this->param['partner_id']);
        if (isset($this->param['adsense_id']) && $this->param['adsense_id'] != '') {
            $this->save($this->param, [
                'adsense_id' => $this->param['adsense_id']
            ]);
        } else {
            $this->allowField(true)->save($this->param);
        }
        Cache::clear('adsense');
        return 'success';
    }
    // 软删除
    public function ajaxDelete()
    {
        if (isset($this->param['adsense_id'])) {
            if (isset($this->param['delete']) && $this->param['delete'] == 'yes') {
                $this->destroy(function ($query) {
                    $query->where('adsense_id', 'IN', $this->param['adsense_id']);
                }, true);
            } else {
                $this->destroy(function ($query) {
                    $query->where('adsense_id', 'IN', $this->param['adsense_id']);
                }, true);
            }
            Cache::clear('adsense');
            return 'success';
        }
    }
}